package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.qf.pojo.*;
import com.qf.service.OrderService;
import javafx.geometry.Pos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired(required = false)
    private OrderService orderService;

    @GetMapping
    @ResponseBody
    public Message<Order> selectAll(int page,int limit){
        PageHelper.startPage(page,limit);
        List<Order> orders = orderService.selectAll();
        Message<Order> message = new Message<Order>();
        message.setData(orders);
        message.setCount(20);
        return message;
    }

    @PostMapping("/del")
    @ResponseBody
    public int del(HttpServletRequest request){
        String oids = request.getParameter("oid");
        int oid = Integer.valueOf(oids);
        int res = orderService.delete(oid);
        return res;
    }

    @RequestMapping("/allOrder")
    @ResponseBody
    public Msg<Order_info> allOrder(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        int uid = user.getId();
        int page=0;
        page = Integer.valueOf(request.getParameter("page"));
        List<Order_info> order_infos = orderService.allOrder(uid,page);
        Msg<Order_info> msg = new Msg<Order_info>();
        int rows = orderService.getRows(uid);
        msg.setData(order_infos);
        msg.setCode(1);
        msg.setCount(rows);
        return msg;
    }
}
